package com.sendpo.zjx.sendpo.activity;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.lljjcoder.citypickerview.widget.CityPicker;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.services.AmapLocationService;
import com.sendpo.zjx.sendpo.utils.Tools;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/8/9.
 */

public class FabuxingChengActivity extends AppCompatActivity implements AMapLocationListener{

    private Dialog dialog;
    CityPicker cityPicker=null;
    @BindView(value = R.id.content_city)
    public TextView content_city;
    private String fromprovince="";
    private String fromcity="";
    private String fromarea="";
    private String toprovince="";
    private String tocity="";
    private String toarea="";
    private AmapLocationService amapLocationService;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Tools.initStatus(this, R.color.black);
        setContentView(R.layout.activity_fabuxingcheng);
        ButterKnife.bind(this);
        initComponent();
        amapLocationService=new AmapLocationService(this);
        amapLocationService.registerListener(this);
        amapLocationService.setmLocationOption(getDefaultAmapLocationOptions());
        amapLocationService.start();
    }
    public AMapLocationClientOption getDefaultAmapLocationOptions(){
        AMapLocationClientOption mLocationOption=null;
        if(mLocationOption==null){
            mLocationOption = new AMapLocationClientOption();
            //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
            mLocationOption.setLocationCacheEnable(true);
            mLocationOption.setOnceLocation(true);
            mLocationOption.setOnceLocationLatest(false);
            //设置是否返回地址信息（默认返回地址信息）
            mLocationOption.setNeedAddress(true);
            //设置是否强制刷新WIFI，默认为true，强制刷1新。
            mLocationOption.setWifiScan(true);
            mLocationOption.setInterval(1000);
            //单位是毫秒，默认30000毫秒，建议超时时间不要低于8000毫秒。
            mLocationOption.setHttpTimeOut(20000);
        }
        return  mLocationOption;
    }
    private void initComponent() {
        cityPicker = new CityPicker.Builder(this)
                .textSize(20)
                .title("地址选择")
                .backgroundPop(0xa0000000)
                .titleBackgroundColor("#E6212A")
                .titleTextColor("#FFFFFF")
                .confirTextColor("#FFFFFF")
                .cancelTextColor("#FFFFFF")
                .province("广东省")
                .city("深圳市")
                .district("福田区")
                .textColor(Color.parseColor("#000000"))
                .provinceCyclic(true)
                .cityCyclic(false)
                .districtCyclic(false)
                .visibleItemsCount(7)
                .itemPadding(10)
                .onlyShowProvinceAndCity(false)
                .build();
        cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
            @Override
            public void onSelected(String... contents) {
                content_city.setText(contents[0]+contents[1]+contents[2]);
                toprovince=contents[0];
                tocity=contents[1];
                toarea=contents[2];
            }
            @Override
            public void onCancel() {

            }
        });

    }
    @OnClick(value = {R.id.backbt, R.id.bottom, R.id.selectcity_bt})
    public void onMyClick(View view){
        switch (view.getId()){
            case R.id.backbt:
                finish();
                break;
            case R.id.bottom:
                if(TextUtils.isEmpty(content_city.getText().toString().trim())){
                    Toast.makeText(getApplicationContext(),"目的地不能为空",Toast.LENGTH_SHORT).show();
                return;
                }
                Map<String,Object> map=new HashMap<>();
                 map.put("fromprovince",fromprovince);
                map.put("fromcity",fromcity);
                map.put("fromarea",fromarea);
                map.put("toprovince",toprovince);
                map.put("tocity",tocity);
                map.put("toarea",toarea);
                Tools.redirectActivity(this,WantQiangdanActivity.class,map);
                break;
            case R.id.selectcity_bt:
                cityPicker.show();
                break;
        }
    }

    @Override
    public void onLocationChanged(final AMapLocation aMapLocation) {
               if(aMapLocation==null){
                   Toast.makeText(getApplicationContext(),"定位失败，请检查网络配置",Toast.LENGTH_SHORT).show();
                   return;
               }


        fromprovince=aMapLocation.getProvince();
        fromcity=aMapLocation.getCity();
        fromarea=aMapLocation.getDistrict();
    }
}
